<!DOCTYPE html>
<html>
<head>
    <title>BSC 批量操作面板</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .operation-card {
            margin-bottom: 20px;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .balance-display {
            font-size: 1.5rem;
            font-weight: bold;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container mt-4">
        <h1 class="text-center mb-4">BSC 批量操作面板</h1>
        
        <div class="row">
            <div class="col-md-4">
                <div class="card operation-card mb-3">
                    <h3 class="card-header">钱包操作</h3>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">生成钱包数量</label>
                            <input type="number" class="form-control" id="walletCount" value="10">
                        </div>
                        <button class="btn btn-primary w-100" onclick="generateWallets()">生成钱包</button>
                    </div>
                </div>
                
                <div class="card operation-card mb-3">
                    <h3 class="card-header">BNB 操作</h3>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">转账金额 (BNB)</label>
                            <input type="number" class="form-control" id="bnbAmount" value="0.001" step="0.001">
                        </div>
                        <div class="d-grid gap-2">
                            <button class="btn btn-primary" onclick="transferBNB()">批量转账BNB</button>
                            <button class="btn btn-success" onclick="sendBNBToMain()">批量转回主钱包</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4">
                <div class="card operation-card mb-3">
                    <h3 class="card-header">CAKE 操作</h3>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">转账金额 (CAKE)</label>
                            <input type="number" class="form-control" id="cakeAmount" value="100" step="1">
                        </div>
                        <div class="d-grid gap-2">
                            <button class="btn btn-primary" onclick="transferCake()">批量转账CAKE</button>
                            <button class="btn btn-success" onclick="approveSyrupPool()">授权糖浆池</button>
                            <button class="btn btn-info" onclick="stakeCake()">质押CAKE</button>
                            <button class="btn btn-warning" onclick="unstakeCake()">解除质押</button>
                            <button class="btn btn-danger" onclick="sendCakeToMain()">转回主钱包</button>
                        </div>
                    </div>
                </div>
                
                <div class="card operation-card mb-3">
                    <h3 class="card-header">BGSC 操作</h3>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <button class="btn btn-primary" onclick="harvestBgsc()">收割BGSC</button>
                            <button class="btn btn-success" onclick="sendBgscToMain()">转回主钱包</button>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-4">
                <div class="card operation-card mb-3">
                    <h3 class="card-header">钱包余额</h3>
                    <div class="card-body">
                        <button class="btn btn-info w-100 mb-3" onclick="getAllBalances()">刷新所有余额</button>
                        
                        <div class="balance-display p-3 bg-light rounded">
                            <h5 class="border-bottom pb-2">主钱包</h5>
                            <div class="mb-2">BNB: <span id="mainWalletBalance">0</span></div>
                            <div class="mb-2">CAKE: <span id="mainWalletCake">0</span></div>
                            <div>BGSC: <span id="mainWalletBgsc">0</span></div>
                        </div>
                        
                        <div id="walletsBalances" class="mt-3" style="max-height: 400px; overflow-y: auto; width: 100%;">
                            <!-- 钱包余额将在这里动态显示 -->
                        </div>
                    </div>
                </div>
                
                <div class="card operation-card">
                    <h3 class="card-header">操作日志</h3>
                    <div class="card-body p-0">
                        <div id="operationLog" style="height: 300px; overflow-y: auto; padding: 10px; background-color: #f8f9fa;"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.7.2/socket.io.min.js"></script>
    <script>
        function log(message) {
            const logElement = document.getElementById('operationLog');
            logElement.innerHTML += `<div>${new Date().toLocaleTimeString()}: ${message}</div>`;
            logElement.scrollTop = logElement.scrollHeight;
        }

        async function apiCall(endpoint, data = {}) {
            try {
                const response = await fetch(endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: new URLSearchParams(data)
                });
                return await response.json();
            } catch (error) {
                log(`API调用错误: ${error}`);
                return {success: false};
            }
        }

        function generateWallets() {
            const count = document.getElementById('walletCount').value;
            log(`正在生成 ${count} 个钱包...`);
            
            apiCall('/api/generate_wallets', {count: count})
                .then(response => {
                    if (response.success) {
                        log(`成功生成 ${count} 个钱包`);
                    } else {
                        log('钱包生成失败');
                    }
                });
        }

        function transferBNB() {
            const amount = document.getElementById('bnbAmount').value;
            log(`正在批量转账 ${amount} BNB...`);
            
            apiCall('/api/transfer_bnb', {amount: amount})
                .then(response => {
                    if (response.success) {
                        log(`成功批量转账 ${amount} BNB`);
                    } else {
                        log('BNB转账失败');
                    }
                });
        }

        function sendBNBToMain() {
            log('正在将BNB批量转回主钱包...');
            
            apiCall('/api/send_bnb_to_main')
                .then(response => {
                    if (response.success) {
                        log('成功将BNB批量转回主钱包');
                        getBalance();
                    } else {
                        log('BNB转回主钱包失败');
                    }
                });
        }

        function transferCake() {
            const amount = document.getElementById('cakeAmount').value;
            log(`正在批量转账 ${amount} CAKE...`);
            
            apiCall('/api/transfer_cake', {amount: amount})
                .then(response => {
                    if (response.success) {
                        log(`成功批量转账 ${amount} CAKE`);
                    } else {
                        log('CAKE转账失败');
                    }
                });
        }

        function approveSyrupPool() {
            log('正在授权糖浆池...');
            
            apiCall('/api/approve_syrup_pool')
                .then(response => {
                    if (response.success) {
                        log('成功授权糖浆池');
                    } else {
                        log('糖浆池授权失败');
                    }
                });
        }

        function stakeCake() {
            const amount = document.getElementById('cakeAmount').value;
            log(`正在质押 ${amount} CAKE...`);
            
            apiCall('/api/stake_cake', {amount: amount})
                .then(response => {
                    if (response.success) {
                        log(`成功质押 ${amount} CAKE`);
                    } else {
                        log('CAKE质押失败');
                    }
                });
        }

        function harvestBgsc() {
            log('正在收割BGSC...');
            
            apiCall('/api/harvest_bgsc')
                .then(response => {
                    if (response.success) {
                        log('成功收割BGSC');
                    } else {
                        log('BGSC收割失败');
                    }
                });
        }

        function unstakeCake() {
            const amount = document.getElementById('cakeAmount').value;
            log(`正在解除质押 ${amount} CAKE...`);
            
            apiCall('/api/unstake_cake', {amount: amount})
                .then(response => {
                    if (response.success) {
                        log(`成功解除质押 ${amount} CAKE`);
                    } else {
                        log('CAKE解除质押失败');
                    }
                });
        }

        function sendCakeToMain() {
            const amount = document.getElementById('cakeAmount').value;
            log(`正在将CAKE批量转回主钱包...`);
            
            apiCall('/api/send_cake_to_main', {amount: amount})
                .then(response => {
                    if (response.success) {
                        log('成功将CAKE批量转回主钱包');
                    } else {
                        log('CAKE转回主钱包失败');
                    }
                });
        }

        function sendBgscToMain() {
            log('正在将BGSC批量转回主钱包...');
            
            apiCall('/api/send_bgsc_to_main')
                .then(response => {
                    if (response.success) {
                        log('成功将BGSC批量转回主钱包');
                    } else {
                        log('BGSC转回主钱包失败');
                    }
                });
        }

        function getBalance() {
            fetch('/api/get_balance')
                .then(response => response.json())
                .then(data => {
                    document.getElementById('mainWalletBalance').textContent = data.balance;
                    log('主钱包余额已刷新');
                });
        }

        function getAllBalances() {
            log('正在获取所有钱包余额...');
            fetch('/api/get_all_balances')
                .then(response => response.json())
                .then(data => {
                    const walletsBalances = document.getElementById('walletsBalances');
                    walletsBalances.innerHTML = '';
                    
                    data.balances.forEach((wallet, index) => {
                        const walletDiv = document.createElement('div');
                        walletDiv.className = 'mb-3 p-2 border rounded';
                        walletDiv.style.backgroundColor = wallet.is_main ? '#f8f9fa' : '';
                        
                        const title = wallet.is_main ? '主钱包' : `钱包 #${index + 1} (${wallet.address.slice(0, 6)}...${wallet.address.slice(-4)})`;
                        walletDiv.innerHTML = `
                            <h5>${title}</h5>
                            BNB: ${wallet.bnb.toFixed(6)}<br>
                            CAKE: ${wallet.cake.toFixed(2)}<br>
                            BGSC: ${wallet.bgsc.toFixed(2)}
                        `;
                        
                        walletsBalances.appendChild(walletDiv);
                        
                        if (wallet.is_main) {
                            document.getElementById('mainWalletBalance').textContent = wallet.bnb.toFixed(6);
                            document.getElementById('mainWalletCake').textContent = wallet.cake.toFixed(2);
                            document.getElementById('mainWalletBgsc').textContent = wallet.bgsc.toFixed(2);
                        }
                    });
                    
                    log('所有钱包余额已刷新');
                });
        }
        
        // 初始化Socket.IO连接
        const socket = io();
        
        // 监听日志消息
        socket.on('log_message', function(data) {
            log(data.message);
        });
        
        // 初始化时获取所有余额
        getAllBalances();

        // 初始化时获取余额
        getBalance();
    </script>
</body>
</html>